package com.lane.core.utils;

import java.security.MessageDigest;

/**
 * @author 张先宙 E-mail: zhangxianzhou_std@163.com
 * @version 1.0 创建时间：Jun 29, 2009 10:37:52 PM
 * 
 */
public class MD5Util {

	/**
	 * MD5加密
	 * 
	 * @param str
	 * @return
	 */
	public static String crypt(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5"); // 所用的加密算法
			md.update(str.getBytes());
			byte[] digestArr = md.digest(); // 计算摘要,加密
			String aaa = byte2hex(digestArr);
			return aaa;
		} catch (Exception e) {
			return "error";
		}
	}

	/**
	 * 加密辅助
	 * 
	 * @param b
	 * @return
	 */
	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs = hs + "0" + stmp;
			} else {
				hs = hs + stmp;
			}
		}
		return hs.toUpperCase();
	}
}